<template>
	<div>
		<van-popup :show="show" position="bottom" @click="onpopup" round :close-on-click-overlay="true" @close="show=false" closeable>
			<div class="register-wrapper">
				<image class="logo" src="https://rzkeji.oss-cn-shenzhen.aliyuncs.com/web/bbMachine/popup-logo.png" mode="widthFix"></image>
				<div class="tip">
					我们需要你的手机号来关联账号
				</div>
				<button class="btn" open-type="getPhoneNumber" @getphonenumber="getPhoneNumber" >获取手机码号</button>
			</div>
		</van-popup>
	</div>
</template>

<script>
	export default{
		// props:{
		// 	showPopup:{
		// 		default:false,
		// 		type:Boolean
		// 	}
		// },
		data(){
			return{
				show:false,
				open_id:'',
				tel:''
			}
		},
		// watch:{
		// 	showPopup(nval){
		// 		this.show=showPopup
		// 	}
		// },
		methods:{
			onpopup(){
			    if (this.show){
			        this.show = false;
			    } else {
			        this.show = true;
			    }
			
			},
			getPhoneNumber(e){
				if(!e.detail.encryptedData){
					uni.showToast({
						icon:'none',
						title:'登录失败'
					})
					this.show=false;
					return
				}
			      const _this=this;
			       wx.checkSession({
			        async fail(){
			            wx.login({
			              success: async function(e){
			                const code=e.code;
			                const res=await _this.$api.getWeChatPhone({
			                  open_id:_this.open_id,
			                  iv:e.detail.iv,
			                  encryptedData:e.detail.encryptedData,
			                  code:code
			                })
			                if(res.data.level=='success'){
			                  _this.tel=JSON.parse(res.data.data).phoneNumber;
							  _this.register()
			                }else{
								uni.showToast({
									icon:'none',
									title:res.data.message
								})
			                }
			                
			              },
			              fail(e){
			                console.log(e);
			              }
			            })
			        },
			        success:async function(){
					  const res=await _this.$api.getWeChatPhone({
					    open_id:_this.open_id,
					    iv:e.detail.iv,
					    encryptedData:e.detail.encryptedData,
					  })
			          if(res.data.level=='success'){
			              _this.tel=JSON.parse(res.data.data).phoneNumber;
						  _this.register()
			            }else{
			              uni.showToast({
			              	icon:'none',
			              	title:res.data.message
			              })
			            }
			        }
			      })
			      
			    },
				 async register(){
					  let params={phone:this.tel,open_id:this.open_id};
					  let res=await this.$api.bindingPhoneV2(params);
				    if(res.data.level=="success"){
						uni.setStorageSync('userToken',res.data.data);
						this.show=false;
						this.toLastPage()
				    }else{
				      uni.showToast({
				      	icon:'none',
							title:res.data.message
				      })
				    }
				  },
				  toLastPage(){
				  	//返回授权前的页面
				  	let pageInfo=this.$bus.$data.historyUrl
				  	if(
				  	pageInfo.path!=='pages/index/index'&&
				  	pageInfo.path!== 'pages/circlebar/circlebar' &&
				  	pageInfo.path!== 'pages/notice/notice' &&
				  	pageInfo.path!== 'pages/person/person'
				  	){
				  		
				  		let options=null
				  		if(pageInfo.options){
				  			options=this.$currency.toQueryString(pageInfo.options)
				  		}
				  		uni.redirectTo({
				  			url: `/${pageInfo.path}?${options}`
				  		});
				  	}else{
				  		uni.switchTab({
				  			url:`/${pageInfo.path}`
				  		})
				  	}
				  	
				  },
		}
	}
</script>

<style lang="scss">
	@import '~@/static/scss/mixin.scss';
	.register-wrapper{
		min-height: 500rpx;
		@include flexWrap();
		.logo{
			width: 300rpx;
			
		}
		.tip{
			margin: 10rpx auto;
			width: 100%;
			text-align: center;
		}
		.btn{
			width: 280rpx;
			height: 80rpx;
			line-height: 80rpx;
			font-size: 28rpx;
			color:#000;
			background: rgba(255,226,38,1);
		}
	}
</style>
